<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="码表" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          placeholder="请输入"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button  @click="addData">新建</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="表名" prop="tableName"></el-table-column>
      <el-table-column label="列名（编码）" prop="columnCode"></el-table-column>
      <el-table-column label="列名（编码说明）" prop="columnTxt"></el-table-column>
      <el-table-column label="表说明" prop="txt"></el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row, 'edit')" ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogFormVisible" :title="handleType ==='edit'?'修改码表': '新建码表'" width="500">
      <el-form :model="dialogForm">
        <el-form-item v-show="false" label="表id" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.listId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="表名" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.tableName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="列名（编码）" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.columnCode" autocomplete="off" />
        </el-form-item>
        <el-form-item label="列名（编码说明）" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.columnTxt" autocomplete="off" />
        </el-form-item>
        <el-form-item label="表说明" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.txt" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">
            保存
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="TableConfgig">
import { tableQuery, tableSave, tableDelete } from '@/api/safeguard/index.js';
const queryParams = ref({
  tableName: ''
})
const showSearch = ref(true);
const loading = ref(false);
const tableData = ref([]);

const handleType = ref('')
const dialogFormVisible = ref(false)
const dialogForm = ref({})
const formLabelWidth = '140px'

function getList () {
  const data = queryParams.value.tableName
  loading.value = true;
  tableData.value = []
  tableQuery(data).then(res => {
    if(Array.isArray(res.data)) {
      tableData.value = res.data
    } else {
      tableData.value.push(res.data)
    }
  }).finally(() => {
    loading.value = false;
  })
}

function handleQuery() {
  getList();
}

 /** 添加 */
 function addData () {
  dialogFormVisible.value = true
  dialogForm.value = {}
}
 /** 编辑 */
function handleUpdate (row, type) {
  handleType.value = type
  dialogForm.value = row
  dialogFormVisible.value = true
}

function handleSave() {  
  // 保存成功刷新数据
  tableSave({...dialogForm.value}).then(() => {
    dialogFormVisible.value = false
    getList();
  })
}

function handleDelete(row){
  const listId = row.listId || 0
  tableDelete(listId).then(() => {
    dialogFormVisible.value = false
    getList();
  })
}

handleQuery()
</script>